package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.RepairShop;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.RepairShopService;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class RepairShopController {

	//自动注入
	@Autowired
	private RepairShopService repairShopService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	/**
	 * 查询修理厂
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryRepairShop/look.action")
    public String queryRepairShop(HttpServletRequest request, String flag) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//查询修理厂
		List<RepairShop> repairShops = repairShopService.queryRepairShopAll();
		//把数据传到页面
		request.setAttribute("repairShops", repairShops);
        request.setAttribute("flag", flag);
        return "/backHome/basicData/repairShops/repairShopsList";
	}

    /**
     * @Author lcx
     * @Date 2017/12/15 0015 下午 3:43
     * ajax查询修理厂
     */
    @RequestMapping("/select_RepairShop")
    @ResponseBody
	public List<RepairShop> selectRepairShop(HttpServletRequest request, String name) {
        //查询修理厂
		List<RepairShop> repairShops = repairShopService.queryRepairShopAllAsName(name);
        //把数据传到页面
        request.setAttribute("repairShops", repairShops);
        return repairShops;
    }

    /**
     * 根据简称查询
	 * @param request
	 * @param fsimplename 简称
	 * @return
	 */
	@RequestMapping("/queryRepairShopByFsimplename")
	public String queryRepairShopByFsimplename(HttpServletRequest request,String fsimplename){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		if("".equals(fsimplename)){
            queryRepairShop(request, null);//如果为空就调用查询页面
        }else{
            fsimplename = fsimplename.trim();//去空格
            //查询修理厂
			List<RepairShop> repairShops = repairShopService.queryRepairShopByFdescriptionL2(user.getFcompanyid(), fsimplename);
			//把数据传到页面
			request.setAttribute("repairShops", repairShops);
			request.setAttribute("fsimplename", fsimplename);
		}
		return "/backHome/basicData/repairShops/repairShopsList";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertRepairShop")
	public String insertRepairShop(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取上一个编码
        /*String number = repairShopService.selectFnumber(user.getFcompanyid());*/
		//获取编码
        String fnumber = GetFnumber.fnumbers("XLC");
		//获取驾校
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("fnumber", fnumber);
		request.setAttribute("department", department);
		request.setAttribute("type", type);
		return "/backHome/basicData/repairShops/addRepairShops";
	}
	
	/**
	 * 添加修理厂
	 * @param request
	 * @param repairShop 属性自动封装
	 * @return
	 */
	@RequestMapping("/addRepairShop")
	public String addRepairShop(HttpServletRequest request,RepairShop repairShop){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//利用UUID生成fid
		repairShop.setFid(GetFid.UUIDIntoFid());
		//控制单元id
		repairShop.setFcontrolunitid(user.getFcompanyid());
		//创建人id
		repairShop.setFcreatorid(user.getFid());
	    //获取当前时间
		String createTime = TimeFormat.timeFormat(new Date());
		//当前创建时间
		repairShop.setFcreatetime(createTime);
		//添加
		repairShopService.addRepairShop(repairShop);
		 //添加日志
	    customLogService.addLog("车辆基础资料", createTime+"对修理厂进行添加操作", null, user.getFid());
	    //重定向到添加方法
	    return "redirect:/DrivingSys/insertRepairShop?type=3";
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid 要查询的fid
	 * @return
	 */
	@RequestMapping("/selectRepairShopByFid")
	public String selectRepairShopByFid(HttpServletRequest request,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取驾校
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		//查询到的数据
		RepairShop repairShop = repairShopService.queryRepairShopByFid(fid);
		//把数据传到页面
		request.setAttribute("department", department);
		request.setAttribute("repairShop", repairShop);
		return "/backHome/basicData/repairShops/updateRepairShops";
	}
	
	/**
	 * 修改修理厂
	 * @param request
	 * @param repairShop 属性自动封装
	 * @return
	 */
	@RequestMapping("/modifyRepairShop")
	public String modifyRepairShop(HttpServletRequest request,RepairShop repairShop){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//控制单元id
		repairShop.setFcontrolunitid(user.getFcompanyid());
		//公司id
	    repairShop.setCfficompanyid(user.getFcompanyid());
		//修改人id
	    repairShop.setFlastupdateuserid(user.getFid());
	    //获取当前时间
		String updateTime = TimeFormat.timeFormat(new Date());
		//当前修改时间
		repairShop.setFlastupdatetime(updateTime);
		//修改
		repairShopService.modifyRepairShop(repairShop);
		 //添加日志
	    customLogService.addLog("车辆基础资料", updateTime+"对修理厂进行修改操作", null, user.getFid());
	    //调用查询
        return commonController.skipUrl("/queryRepairShop/look.action");
    }
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId 要删除的fid，可以多个
	 */
	@RequestMapping("/deleteRepairShopByFid")
	public String deleteRepairShopByFid(HttpServletRequest request,String deleteId[],String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//删除
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				repairShopService.deleteRepairShopByFid(deleteId[i]);
			}
		}else{
			repairShopService.deleteRepairShopByFid(fid);
		}
		
		 //添加日志
	    customLogService.addLog("车辆基础资料", TimeFormat.timeFormat(new Date())+"对修理厂进行删除操作", null, user.getFid());
	    //调用查询
        queryRepairShop(request, null);
        return "/backHome/basicData/repairShops/repairShopsList";
	}



	/**
	 * 导出Execl
	 *
	 * @param response
	 */
	@RequestMapping("/importRepairShop")
	public void importTrainType(HttpServletResponse response, HttpServletRequest request, String fsimplename) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		List<Object> data = new ArrayList<>();
		//查询数据
		List<RepairShop> repairShops = null;
		if("".equals(fsimplename)){
			repairShops = repairShopService.queryRepairShopByFcontrolunitid(user.getFcompanyid());
		}else{
			fsimplename = fsimplename.trim();//去空格
			//查询修理厂
            repairShops = repairShopService.queryRepairShopByFdescriptionL2(user.getFcompanyid(), fsimplename.trim());
        }
		for (RepairShop repairShop : repairShops) {
			data.add(repairShop.getFnumber());//编号
			data.add(repairShop.getFnameL2());//名称
			data.add(repairShop.getFsimplename());//班级简称
			data.add(repairShop.getFdescriptionL2());//描述
			data.add(repairShop.getFcreatetime());//创建时间
			data.add(repairShop.getFlastupdatetime());//最后修改时间
			data.add(repairShop.getCreateName());//创建人
			data.add(repairShop.getUpdateName());//修改人
		}

		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间","创建人", "修改人"};
		//长度
		Integer length[] = {5000, 7000, 5000, 5000, 6200, 6200, 3000, 3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "修理厂" + TimeFormat.getNowTime2() + ".xls", "修理厂", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
